package com.example.mall.controller;

import com.example.mall.common.api.CommonPage;
import com.example.mall.common.api.CommonResult;
import com.example.mall.mbg.model.Screen;
import com.example.mall.service.ScreenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * ScreenController
 * Created by on 2023/12/27.
 */
@Api(tags = "ScreenController", description = "页面管理")
@Controller
@RequestMapping("/screen")
public class ScreenController {
    @Autowired
    private ScreenService screenService;

    private static final Logger LOGGER = LoggerFactory.getLogger(ScreenController.class);

    @ApiOperation("获取所有页面列表")
    @RequestMapping(value = "listAll", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<List<Screen>> getScreenList() {
        return CommonResult.success(screenService.listAll());
    }

    @ApiOperation("添加页面")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult createScreen(@RequestBody Screen screen) {
        CommonResult commonResult;
        int count = screenService.create(screen);
        if (count == 1) {
            commonResult = CommonResult.success(screen);
            LOGGER.debug("createScreen success:{}", screen);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("createScreen failed:{}", screen);
        }
        return commonResult;
    }

    @ApiOperation("更新指定id页面信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult updateScreen(@PathVariable("id") Integer screenid, @RequestBody Screen screen, BindingResult bindingResult) {
        CommonResult commonResult;
        int count = screenService.update(screenid, screen);
        if (count == 1) {
            commonResult = CommonResult.success(screen);
            LOGGER.debug("updateScreen success:{}", screen);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("updateScreen failed:{}", screen);
        }
        return commonResult;
    }

    @ApiOperation("删除指定id的页面")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult deleteScreen(@PathVariable("id") Integer screenid) {
        CommonResult commonResult;
        int count = screenService.delete(screenid);
        if (count == 1) {
            commonResult = CommonResult.success(null);
            LOGGER.debug("deleteScreen success:id={}", screenid);
        } else {
            commonResult = CommonResult.failed("操作失败");
            LOGGER.debug("deleteScreen failed:id={}", screenid);
        }
        return commonResult;
    }

    @ApiOperation("分页查询页面列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<CommonPage<Screen>> listScreen(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                        @RequestParam(value = "pageSize", defaultValue = "3") Integer pageSize) {
        List<Screen> screenList = screenService.list(pageNum, pageSize);
        return CommonResult.success(CommonPage.restPage(screenList));
    }

    @ApiOperation("获取指定id的页面详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<Screen> screen(@PathVariable("id") Integer screenid) {
        return CommonResult.success(screenService.get(screenid));
    }
}
